package fr.esiag.isidis.epp.repository;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import au.com.bytecode.opencsv.CSVReader;
import fr.esiag.isidis.epp.model.TemperatureModel;
import fr.esiag.isidis.epp.util.Utils;

@Repository
public class TemperatureRepository {

		private static Logger logger = Logger.getLogger(TemperatureRepository.class);
		
		@Value("${batch.in.temperature.file}")
		private String temperature_pathCsvFile;
		
		@Autowired
		private JdbcTemplate jdbcTemplate;

		public int loadTemperature(TemperatureModel tempItem)
		{
				
			/* traitement cdc */
			int date = Utils.convertDateToInt(tempItem.getDateAndHour());
			int heure = Utils.convertHourToInt(tempItem.getDateAndHour());
			int id;
			String idnn;
			float temperature;
			Vector<Float> tabtemp = tempItem.getTemperature();
			
//			CREATE TABLE TEMPERATURE(
//					IdSite 		VARCHAR(8) NOT NULL,
//					TDate 		int NOT NULL,
//					THour 		int NOT NULL,
//					Temprature 	Float,
//					PRIMARY KEY (IdSite,TDate,THour),
//					CONSTRAINT FK_temp_site FOREIGN KEY(IdSite) REFERENCES Site(IdSite),
//					INDEX temperature_index (TDate)
//				);
			
			int result = 0;
			for(id=0; id< 42; id++){
				idnn = Utils.getIdnn(id);
				if(tabtemp.get(id)!= null){
					temperature = tabtemp.get(id);
					Object[] params = {idnn,date,heure,temperature};
					result += jdbcTemplate.update(SqlConstants.QUEYR_INSERT_INTO_TEMPERATURE, params);
				}
				else{
					Object[] params = {idnn,date,heure,null};
					result += jdbcTemplate.update(SqlConstants.QUEYR_INSERT_INTO_TEMPERATURE, params);
				}

			}
			return result;
		}
		
		
		public List<TemperatureModel> temperature_extractFromCsvFile() throws Exception
		{
			List<TemperatureModel> retour = new ArrayList<TemperatureModel>();
			
			// Traitement
			CSVReader reader = new CSVReader(new FileReader(temperature_pathCsvFile),';');
			// Header
			String[] nextLine = reader.readNext();
			nextLine = reader.readNext();
			
			while(nextLine != null)
			{
				
				TemperatureModel item = temperature_transformeFromCsvLine(nextLine);
				if(item != null){
				retour.add(item);
				}
				logger.debug("temperature "+item);
				nextLine = reader.readNext();
			}
			reader.close();
			return retour;
		}
		
		private TemperatureModel temperature_transformeFromCsvLine(String[] items)
		{
			TemperatureModel retour = new TemperatureModel();
			if(!items[0].equals("")){
			retour.setDateAndHour(items[0]);
			}else
				return null;
			Vector<Float> vect = new Vector<Float>();
			int i;
			for (i= 1; i<=43; i++){
				String a = items[i];
				if ( !a.equals("")){
					a = Utils.virguleTopoint(a);
					vect.add(Float.parseFloat(a));
				}
				else
					vect.add(null);
			}
			retour.setTemperature(vect);
			return retour;
		}
		
}
